package com.benmei.ntlive.vo;

import com.benmei.ntlive.common.CourseType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Peter on 2017/9/12.
 */
public class Room {
    private static Logger logger = LoggerFactory.getLogger(Room.class);
    private Integer roomId;
    private String roomType;// 1v1、1vn
    private List<Session> students = new ArrayList<Session>();
    private List<Session> teachers = new ArrayList<Session>();
    private List<HandsUpUser> handsUpList = new ArrayList<HandsUpUser>();

    public Room(Integer roomId, String roomType) {
        this.roomId = roomId;
        this.roomType = roomType;
    }

    // 举手队列添加一个用户
    public void addHandsUpUser(User user) {
        boolean isHandUp = false;// 该用户是否已经举手
        for (HandsUpUser oldUser : handsUpList) {
            if (oldUser.getMembId().intValue() == user.getId().intValue()) {
                isHandUp = true;
                break;
            }
        }

        if (isHandUp) {
            logger.info("用户举手【举手队列已存在】：[" + user.getId() + "]" + user.getName());
        } else {
            handsUpList.add(new HandsUpUser(user.getAgora_uid(),user.getId(), user.getName(), user.getAvatar()));
            logger.info("用户举手【举手队列添加成功】：[" + user.getId() + "]" + user.getName());
        }
        logger.info("用户举手，当前举手人数：" + handsUpList.size());
    }

    // 举手队列移除一个用户
    public void removeHandsUpUser(User user) {
        handsUpList.remove(new HandsUpUser(user.getAgora_uid(),user.getId(), user.getName(), user.getAvatar()));
    }

    public void removeStudent(Session session) {
        boolean result = this.getStudents().remove(session);
        this.removeHandsUpUser(session.getUser());// 举手队列中移除该学生
        logger.info("room 移除学生[{}]：[{}][{}]", result, session.getUser().getId(), session.getChannel().remoteAddress().toString());
    }

    public List<HandsUpUser> getHandsUpList() {
        return handsUpList;
    }

    public void setHandsUpList(List<HandsUpUser> handsUpList) {
        this.handsUpList = handsUpList;
    }

    public String getRoomType() {
        return roomType;
    }

    public int getCourseTypeCode() {
        return CourseType.getByName(roomType).getCode();
    }

    public void setRoomType(String roomType) {
        this.roomType = roomType;
    }

    public Integer getRoomId() {
        return roomId;
    }

    public void setRoomId(Integer roomId) {
        this.roomId = roomId;
    }

    public List<Session> getStudents() {
        return students;
    }

    public void setStudents(List<Session> students) {
        this.students = students;
    }

    public List<Session> getTeachers() {
        return teachers;
    }

    public void setTeachers(List<Session> teachers) {
        this.teachers = teachers;
    }

    public boolean isEmpty() {
        return (this.teachers.isEmpty() && this.students.isEmpty());
    }

    // 根据学生id获取学生对应的Session
    public Session getStudentByMembId(Integer memb_id) {
        Session student = null;
        for (Session stu : this.getStudents()) {
            if (stu.getUser().getId().intValue() == memb_id.intValue()) {
                student = stu;
            }
        }
        return student;
    }

    // 根据ip和端口号获取学生Session
    public Session getStudentByEndpoint(String endpoint) {
        Session student = null;
        for (Session stu : this.getStudents()) {
            if (stu.getChannel().remoteAddress().toString().equals(endpoint)) {
                student = stu;
            }
        }
        return student;
    }

    // 根据ip和端口号获取老师Session
    public Session getTeacherByEndpoint(String endpoint) {
        Session teacher = null;
        for (Session tea : this.getTeachers()) {
            if (tea.getChannel().remoteAddress().toString().equals(endpoint)) {
                teacher = tea;
            }
        }
        return teacher;
    }

    public void addStudent(Session student) {
        this.getStudents().add(student);
    }

    public Session getTeacherByTeaId(Integer tea_id) {
        Session teacher = null;
        for (Session tea : this.getTeachers()) {
            if (tea.getUser().getId().intValue() == tea_id.intValue()) {
                teacher = tea;
            }
        }
        return teacher;
    }

    public void addTeacher(Session teacher) {
        this.getTeachers().add(teacher);
    }


}
